package com.ddstation.onsitelaw.service;

import com.ddstation.onsitelaw.entity.DtHealthManageInstitutionEntity;
import com.ddstation.onsitelaw.entity.DtInvsManageInstitutionEntity;
import com.ddstation.onsitelaw.entity.DtPrescriptionTemplateManageEntity;
import com.ddstation.onsitelaw.entity.auditentity.DdDoctorEntity;
import com.ddstation.onsitelaw.mapper.DtHealthManageInstitutionMapper;
import com.ddstation.onsitelaw.mapper.DtInvsManageInstitutionMapper;
import com.ddstation.onsitelaw.mapper.DtPrescriptionTemplateManageMapper;
import com.ddstation.onsitelaw.mapper.ViolationRecordMapper;
import com.ddstation.onsitelaw.model.DoctorModel;
import com.ddstation.onsitelaw.model.DtPrescriptionTemplateManageModel;
import com.ddstation.onsitelaw.model.HealthInstitutionModel;
import com.ddstation.onsitelaw.model.ViolationManageModel;
import com.ddstation.onsitelaw.utils.AliCloudOssUtil;
import com.ddstation.onsitelaw.utils.CopyBeanUtil;
import com.ddstation.onsitelaw.utils.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 忆秋枫
 */
@Service
public class DtHealthManageInstitutionServiceImpl implements DtHealthManageInstitutionService {

    @Inject
    private DtHealthManageInstitutionMapper dtHealthManageInstitutionMapper;

    @Inject
    private DtInvsManageInstitutionMapper dtInvsManageInstitutionMapper;


    @Inject
    private DtPrescriptionTemplateManageMapper dtPrescriptionTemplateManageMapper;


    @Inject
    private ViolationRecordMapper violationRecordMapper;

    @Override
    @Transactional(readOnly = true)
    public List<Map<String, Object>> selectByAddress(String province, String city, String county) {
        return dtHealthManageInstitutionMapper.selectByAddress(province, city, county);
    }

    @Override
    @Transactional(readOnly = true)
    public PageResult selectAllHealthManageList(HashMap paramMap) {
        PageHelper.startPage(Integer.parseInt(paramMap.get("pageNum").toString()), Integer.parseInt(paramMap.get("pageSize").toString()));
        List<DtHealthManageInstitutionEntity> institutionList = dtHealthManageInstitutionMapper.selectAllHealthManageList(paramMap);
        PageInfo<DtHealthManageInstitutionEntity> info = new PageInfo<>(institutionList);
        return new PageResult(info, institutionList);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveInstitutionalInfo(HealthInstitutionModel healthInstitutionModel) {
        DtInvsManageInstitutionEntity dtInvsManageInstitutionEntity = dtInvsManageInstitutionMapper.selectById(healthInstitutionModel.getAreaCode());
        healthInstitutionModel.setProvince(dtInvsManageInstitutionEntity.getProvince());
        healthInstitutionModel.setCity(dtInvsManageInstitutionEntity.getCity());
        healthInstitutionModel.setCounty(dtInvsManageInstitutionEntity.getCounty());
        dtHealthManageInstitutionMapper.saveInstitutionalInfo(healthInstitutionModel);
    }

    @Override
    public PageResult getHealthManageList(HashMap paramMap) {
        Example example = new Example(DtPrescriptionTemplateManageEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("institutionId", "");
        criteria.andNotEqualTo("templateSign", "-1");
        List<DtPrescriptionTemplateManageEntity> entities = dtPrescriptionTemplateManageMapper.selectByExample(example);
        for (DtPrescriptionTemplateManageEntity entity : entities) {
            entity.setTemplatePicUrl(AliCloudOssUtil.getOriginImgUrl(null, entity.getTemplatePicUrl()));
        }
        Map<String, List<DtPrescriptionTemplateManageEntity>> templateMap = entities.stream().collect(Collectors.groupingBy(DtPrescriptionTemplateManageEntity::getInstitutionId));


        List<ViolationManageModel> uploads = violationRecordMapper.getManageUploadNum();
        Map<String, String> uploadsMap = uploads.stream().collect(Collectors.toMap(ViolationManageModel::getInstitutionId, ViolationManageModel::getViolationNum));

        List<ViolationManageModel> violations = violationRecordMapper.getManageViolationNum();
        Map<String, String> violationsMap = violations.stream().collect(Collectors.toMap(ViolationManageModel::getInstitutionId, ViolationManageModel::getViolationNum));

        PageHelper.startPage(Integer.parseInt(paramMap.get("pageNum").toString()), Integer.parseInt(paramMap.get("pageSize").toString()));
        List<DtHealthManageInstitutionEntity> institutionList = dtHealthManageInstitutionMapper.getHealthManageList(paramMap);
        PageInfo<DtHealthManageInstitutionEntity> info = new PageInfo<>(institutionList);
        if (!ObjectUtils.isEmpty(templateMap)) {
            institutionList.forEach(item -> {
                item.setUploadNum(StringUtils.isEmpty(uploadsMap.get(item.getId())) ? null : uploadsMap.get(item.getId()));
                item.setViolationNum(StringUtils.isEmpty(violationsMap.get(item.getId())) ? null : violationsMap.get(item.getId()));
                if (!ObjectUtils.isEmpty(templateMap.get(item.getId()))) {
                    List<String> picList = templateMap.get(item.getId()).stream().map(i -> i.getTemplatePicUrl()).collect(Collectors.toList());
                    item.setTemplatePicUrl(picList);
                }
            });
        }
        return new PageResult(info, institutionList);
    }


    @Override
    public void savePrescriptionTemplate(DtPrescriptionTemplateManageModel dtPrescriptionTemplateManageModel) {
        DtPrescriptionTemplateManageEntity entity = CopyBeanUtil.copyObject(dtPrescriptionTemplateManageModel, DtPrescriptionTemplateManageEntity.class);
        dtPrescriptionTemplateManageMapper.insertSelective(entity);
    }

    @Override
    public List<DtPrescriptionTemplateManageModel> selectPrescriptionTemplate(String institutionId, String templateSign) {
        Example example = new Example(DtPrescriptionTemplateManageEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isEmpty(institutionId)) {
            criteria.andEqualTo("institutionId", "");
        } else {
            criteria.andEqualTo("institutionId", institutionId);
        }
        if (StringUtils.isEmpty(templateSign)) {
            criteria.andNotEqualTo("templateSign", "-1");
        } else {
            criteria.andEqualTo("templateSign", templateSign);
        }

        List<DtPrescriptionTemplateManageEntity> entities = dtPrescriptionTemplateManageMapper.selectByExample(example);
        List<DtPrescriptionTemplateManageModel> modelList = CopyBeanUtil.copyObjectList(entities, DtPrescriptionTemplateManageModel.class);
        modelList.forEach(item -> {
            item.setTemplatePicUrl(AliCloudOssUtil.getOriginImgUrl(null, item.getTemplatePicUrl()));
        });
        return modelList;
    }


    @Override
    public DtPrescriptionTemplateManageModel selectPreTempByPrimaryKey(Integer templateId) {
        DtPrescriptionTemplateManageEntity entity = dtPrescriptionTemplateManageMapper.selectByPrimaryKey(templateId);
        return CopyBeanUtil.copyObject(entity, DtPrescriptionTemplateManageModel.class);
    }


    @Override
    public void deletePrescriptionTemplate(String institutionId) {
        Example example = new Example(DtPrescriptionTemplateManageEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("institutionId", institutionId);
        dtPrescriptionTemplateManageMapper.deleteByExample(example);

    }

    @Override
    public List<DoctorModel> getDoctorByInstitutionId(String institutionId) {
        return dtHealthManageInstitutionMapper.getDoctorByInstitutionId(institutionId);
    }

    @Override
    public DdDoctorEntity selectDoctorInfoByKey(String doctorId) {
        return dtHealthManageInstitutionMapper.selectDoctorInfoByKey(doctorId);
    }
}